//
//  PBChoosePayTypeController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/23.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBChoosePayTypeController.h"
#import "PBComfimHomeController.h"
#import "PBComfirmRentTypeCell.h"
#import "PBComfirmChooseCell.h"
#import "PBPayTypeModel.h"
#import "PBOrderHomeTopCell.h"
#import "PBContractItemController.h"

static NSString * const kPBComfirmRentTypeCell = @"PBComfirmRentTypeCell";
static NSString * const kPBComfirmChooseCell = @"PBComfirmChooseCell";
static NSString * const kPBOrderHomeTopCell = @"PBOrderHomeTopCell";

@interface PBChoosePayTypeController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (assign, nonatomic) NSIndexPath *selIndex;
@property (nonatomic, assign) NSInteger type;
@end

@implementation PBChoosePayTypeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}
- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBComfirmRentTypeCell bundle:nil] forCellReuseIdentifier:kPBComfirmRentTypeCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBComfirmChooseCell bundle:nil] forCellReuseIdentifier:kPBComfirmChooseCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBOrderHomeTopCell bundle:nil] forCellReuseIdentifier:kPBOrderHomeTopCell];
}
- (void)configureData {
    [self showHud];
    NSDictionary *param = @{@"houseId":[NSString stringWithFormat:@"%zd",self.model.ID]};
    [TMNetwork requestGetWithParam:param url:@"/item/list" callback:^(TMBaseReqModel *model) {
        [self hideHud];
        if (model.code == 200) {
            self.dataSource = [PBPayTypeModel objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}


#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return self.dataSource.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PBOrderHomeTopCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBOrderHomeTopCell forIndexPath:indexPath];
        [cell refreshWithModel:self.model];
        return cell;
    }else if (indexPath.section == 1) {
        PBComfirmRentTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBComfirmRentTypeCell forIndexPath:indexPath];
        return cell;
    }
    PBComfirmChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBComfirmChooseCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    if (_selIndex == indexPath) {
        cell.mainImageView.image = [UIImage imageNamed:@"single_checked"];
    }else {
        cell.mainImageView.image = [UIImage imageNamed:@"single_check"];
    }
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 0.01f;
    }
    return 15;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        PBComfirmChooseCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
        celled.mainImageView.image = [UIImage imageNamed:@"single_check"];
        _selIndex = indexPath;
        PBComfirmChooseCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.mainImageView.image = [UIImage imageNamed:@"single_checked"];
    }
}


// 确认支付方式
- (IBAction)sureButtonClick:(UIButton *)sender {
    if (!_selIndex) {
        [self showHint:@"请先选择房租支付方式"];
        return;
    }
    PBContractItemController *vc = [[PBContractItemController alloc] init];
    vc.payModel = self.dataSource[_selIndex.row];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"选择支付方式";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
